<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class nutrition extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'passport/index' );
			exit ();
		}
	}
	/*
	 * 返回配方列表
	 */
	public function index() {
    	$this->load->library ( 'pagination' );
    	// 每页显示数据条数
    	$per_page=10;//每页显示10项

    	$config['base_url']=site_url('nutrition/index');
    	$this->load->model ( 'nutrition_model' );

    	// 数据总数
    	$config ['total_rows'] = $this->nutrition_model->count_nutritions();
    	$config ['per_page'] = $per_page;
    	$config ['first_link'] = '第一页';
    	$config ['last_link'] = '最后一页';
    	$config ['next_link'] = '下一页';
    	$config ['prev_link'] = '上一页';
    	$config['uri_segment'] =3;

    	$this->pagination->initialize($config);
    	$data['links']=$this->pagination->create_links();
    	//偏移量
    	$offset=intval($this->uri->segment(3));
    	$data ['nutritions'] = $this->nutrition_model->get_nutritions($per_page,$offset);
    	$data ['title'] = '配方列表 - '.SYSTEM_NAME;
    	$this->load->view ( 'header', $data );
    	$this->load->view ( 'nutrition/index' );
    	$this->load->view ( 'footer' );
	}
	/*
	 * 跳转到添加配方页面
	 */
	public function add() {
		$data ['title'] = '添加配方 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'nutrition/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 跳转到编辑配方页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'nutrition_model' );
		$data ['nutrition'] = $this->nutrition_model->load ( $id );
		$data ['title'] = '编辑配方 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'nutrition/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存配方信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$newnutrition = array (
				'image' => $this->input->post ( 'image' ),
				'name' => $this->input->post ( 'name' ),
				'keshi' => $this->input->post ( 'keshi' ),
				'nengliang' => $this->input->post ( 'nengliang' ),
				'water' => $this->input->post ( 'water' ),
				'danbaizhi' => $this->input->post ( 'danbaizhi' ),
				'zhifang' => $this->input->post ( 'zhifang' ),
				'xianwei' => $this->input->post ( 'xianwei' ),
				'tanshui' => $this->input->post ( 'tanshui' ),
				'weiA' => $this->input->post ( 'weiA' ),
				'weiB1' => $this->input->post ( 'weiB1' ),
				'weiB2' => $this->input->post ( 'weiB2' ),
				'yansuan' => $this->input->post ( 'yansuan' ),
				'weiE' => $this->input->post ( 'weiE' ),
				'na' => $this->input->post ( 'na' ),
				'ca' => $this->input->post ( 'ca' ),
				'fe' => $this->input->post ( 'fe' ),
				'weiC' => $this->input->post ( 'weiC' ),
				'dangu' => $this->input->post ( 'dangu' )
		);
		$this->load->model ( 'nutrition_model' );
		if ($id) {
			if($this->nutrition_model->update ( $id, $newnutrition )){
				return_json(TRUE,'','修改成功！');
			}else{
				return_json(FALSE,'','信息未改动！');
			}
		} else {
			$nutritionid=$this->nutrition_model->create ( $newnutrition );
			if(!empty($nutritionid)){
				return_json(TRUE,'','添加成功！');
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
	}
	/*
	 * 删除配方信息
	 */
	public function delete() {
	    $id = $this->input->post('id');
		$this->load->model ( 'nutrition_model' );
		if ($this->nutrition_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！');
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
    }
	/*
	 *根据关键字得到配方的信息
	 */
	 public function getNutritions() {
    	$name = $this->input->post('name');
    	$this->load->model ( 'nutrition_model' );
    	$data=$this->nutrition_model->get_by_name ( $name );
    	return_json_data($data);
    }

}
